package com.ruijie.autotest.log.validation.paiboftp.wifiap;

import com.google.common.base.CharMatcher;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.ruijie.autotest.log.core.validate.IValidator;
import com.ruijie.autotest.log.impl.validate.SolidValueValidator;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;

import static com.ruijie.autotest.log.impl.validate.SqlExistingValidator.sqlExistingVerificationOf;
import static com.ruijie.autotest.log.impl.validate.SqlMappedExistingValidator.mappedVerification;

@SpringBootConfiguration
public class Config {
	@Bean
	public DataSource dataSource() {
		return new ComboPooledDataSource("remote");
	}

	@Bean
	public IValidator versionValidator() {
		return new SolidValueValidator("1.0.0");
	}

	@Bean
	public IValidator eventTypeValidator() {
		return new SolidValueValidator("44");
	}

	@Bean
	public IValidator docVersionValidator() {
		return new SolidValueValidator("4.3.3");
	}

	@Bean
	public IValidator apMacValidator(DataSource dataSource) {
		return mappedVerification(
				"supervisor_ac_captured_ssid_log.ap_mac",
				Config::convertMacWithBarUpperCase,
				dataSource);
	}

	@Bean
	public IValidator apSsidValidator(DataSource dataSource) {
		return sqlExistingVerificationOf("supervisor_ac_captured_ssid_log.ssid", dataSource);
	}

	@Bean
	public IValidator apChannelValidator(DataSource dataSource) {
		return sqlExistingVerificationOf("supervisor_ac_captured_ssid_log.channel", dataSource);
	}

	@Bean
	public IValidator encryptAlgorithmTypeValidator(DataSource dataSource) {
		return mappedVerification(
				"supervisor_ac_captured_ssid_log.encry_type",
				Config::getEncryptTypeCode,
				dataSource);
	}

	@Bean
	public IValidator captureTimeValidator(DataSource dataSource) {
		return sqlExistingVerificationOf("supervisor_ac_captured_ssid_log.timestamp", dataSource);
	}

	@Bean
	public IValidator apFieldStrengthValidator(DataSource dataSource) {
		return sqlExistingVerificationOf("supervisor_ac_captured_ssid_log.rssi", dataSource);
	}

	@Bean
	public IValidator xCoordinateValidator() {
		return new SolidValueValidator("");
	}

	@Bean
	public IValidator yCoordinateValidator() {
		return new SolidValueValidator("");
	}

	@Bean
	public IValidator netbarWacodeValidator(DataSource dataSource) {
		return sqlExistingVerificationOf("s_location.code", dataSource);
	}

	@Bean
	public IValidator collectionEquipmentIdValidator(DataSource dataSource) {
		return sqlExistingVerificationOf("s_device.code", dataSource);
	}

	@Bean
	public IValidator collectionEquipmentLongitudeValidator(DataSource dataSource) {
		return sqlExistingVerificationOf("s_device.longitude", dataSource);
	}

	@Bean
	public IValidator collectionEquipmentLatitudeValidator(DataSource dataSource) {
		return sqlExistingVerificationOf("s_device.latitude", dataSource);
	}

	@Bean
	public IValidator sourceValidator() {
		return new SolidValueValidator("17");
	}

	@Bean
	public IValidator plastersignValidator() {
		return new SolidValueValidator("0");
	}

	public static String convertMacWithBarUpperCase(String mac) {
		String newMac = null;
		if (null != mac) {
			String tmp = CharMatcher.JAVA_LETTER_OR_DIGIT.retainFrom(mac);
			newMac = Joiner.on("-").join(Splitter.fixedLength(2).split(tmp));
		}
		if (newMac != null) {
			return newMac.toUpperCase();
		} else {
			return null;
		}
	}

	private static String getEncryptTypeCode(String type) {
		String code = "99";
		if (!(type == null || type.trim().length() == 0 || type.equals("") || type.equals("null"))) {
			if (type.equalsIgnoreCase("WEP")) {
				code = "01";
			} else if (type.equalsIgnoreCase("WPA")) {
				code = "02";
			} else if (type.equalsIgnoreCase("WPA2")) {
				code = "03";
			} else if (type.equalsIgnoreCase("PSK")) {
				code = "04";
			}
		}
		return code;
	}
}
